/*
Gamma绑点 page1D
 
Register对应
HBM   page1E
Normal   page20
AOD page2B
 
RGB Gamma值对应  RXX,P1,P2,P3,P4,P5,P6
R  P1,P2
G P3,P4
B P5,P6
*/
#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

u16 ILI8818_Gamma[3][24]; //Gamma值读写缓存区

u8 ILI8818_otptime=0;
const u8 ILI8818_Gamma_Gray[24] =
{	
	255, 247, 225, 198, 177, 159, 134, 114, 104, 93, 81, 65, 51, 41, 35, 29, 22, 17, 13, 9, 5, 3, 1,  0 
};

static void ILI8818_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{ 
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);	
	SSD2828_W_Array(SigMode,channel,buffer,2);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	delay_ms(5);
	buffer[4] = Uart_Error_None;
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}

static void ILI8818_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);	
//	Mipi_Read(pdev,SigMode,channel,3,6,Buffer_ReadCode);   // i 寄存器地址，读取6个
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	buffer[1]=0x02;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}
static void ILI8818_HSRead_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    //bit7 Read Enable:1 – Read operation; bit4 1:HS CLOCK Disable
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data|0x0080);	//SSD2828_W_Reg(SigMode,channel,0xB7,HS_DCS_Short_Read);
	delay_ms(1);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
//    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	
	buffer[1]=2;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);  
}

static void ILI8818_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer0[5];//切换Page_0_R51
	u8 buffer2[5];//切换Page_0_R53
	u8 buffer1[5];
	u8 Gamma_buffer[8];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(5);
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x38;
		buffer1[4] = 0x0c;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer2[0] = 0x02;
		buffer2[1] = 0xe3;
		buffer2[2] = 0x01; //解锁Gamma 写
		SSD2828_W_Array(SigMode,channel,buffer2,0);
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {
        buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x38;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  pag20
		buffer1[3] = 0x38;
		buffer1[4] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
//	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
//	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
////		buffer0[0] = 0x03;
////		buffer0[1] = 0x51;
////		buffer0[2] = 0x01;
////		buffer0[3] = 0x19;
////		SSD2828_W_Array(SigMode,channel,buffer0,0);
////		delay_ms(5);			
//		buffer1[0] = 0x04;
//    buffer1[1] = 0xFF;
//    buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0A;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}
//	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
//	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
////		buffer0[0] = 0x03;
////		buffer0[1] = 0x51;
////		buffer0[2] = 0x01;
////		buffer0[3] = 0x19;
////		SSD2828_W_Array(SigMode,channel,buffer0,0);
////		delay_ms(5);			
//		buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0B;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}	
//	else if((buffer[3]&0x07) == 0x03)	//Normal_Gamma_4
//	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
////		buffer0[0] = 0x03;
////		buffer0[1] = 0x51;
////		buffer0[2] = 0x01;
////		buffer0[3] = 0x19;
////		SSD2828_W_Array(SigMode,channel,buffer0,0);
////		delay_ms(5);			
//		buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0C;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}
	else if((buffer[3]&0x07) == 0x04)	//HBM_Gamma
	{
	    buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x38;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer2[0] = 0x02;
		buffer2[1] = 0x53;
		buffer2[2] = 0xEC;
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(5);
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  page1E
		buffer1[3] = 0x38;
		buffer1[4] = 0x1E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x05)	//AOD_Gamma
	{
	    buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x38;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer2[0] = 0x01;
		buffer2[1] = 0x39; //进AOD
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(5);
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA  page2B
		buffer1[3] = 0x38;
		buffer1[4] = 0x2B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
		
	for( u8 ix = 0;ix<(buffer[2]-1)/2;ix++) 
    {	    
		Gamma_buffer[0]=7;
		Gamma_buffer[1]=ix+1; //写入地址从1开始
		Gamma_buffer[2]=ILI8818_Gamma[0][ix]>>8;
		Gamma_buffer[3]=ILI8818_Gamma[0][ix]&0XFF;	
		Gamma_buffer[4]=ILI8818_Gamma[1][ix]>>8;	
		Gamma_buffer[5]=ILI8818_Gamma[1][ix]&0XFF;	
		Gamma_buffer[6]=ILI8818_Gamma[2][ix]>>8;	
		Gamma_buffer[7]=ILI8818_Gamma[2][ix]&0XFF;	       
        if((buffer[3]&0x70)==0x10)	
		{
			Gamma_buffer[2]=buffer[ix*2+4];	
			Gamma_buffer[3]=buffer[ix*2+1+4];
            ILI8818_Gamma[0][ix]=Gamma_buffer[2]<<8|Gamma_buffer[3];
		}		 
        else if((buffer[3]&0x70)==0x20)
        {		
			Gamma_buffer[4]=buffer[ix*2+4];	
			Gamma_buffer[5]=buffer[ix*2+1+4];
			ILI8818_Gamma[1][ix]=Gamma_buffer[4]<<8|Gamma_buffer[5];
		} 	 
        else if((buffer[3]&0x70)==0x30)
        {
			Gamma_buffer[6]=buffer[ix*2+4];	
			Gamma_buffer[7]=buffer[ix*2+1+4];
            ILI8818_Gamma[2][ix]=Gamma_buffer[6]<<8|Gamma_buffer[7]; 
		}
		  SSD2828_W_Array(SigMode,channel,Gamma_buffer,0);																			
        delay_ms(5);
    }
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x38;
		buffer1[4] = 0x0c;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer2[0] = 0x02;
		buffer2[1] = 0xe3;
	    buffer2[2] = 0x00; //上锁Gamma 写
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);//低速下写完，切换回高速
		buffer[4] = Uart_Error_None;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 
}

 void ILI8818_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	
    u8 buffer0[5];//切换Page_0_R51
	u8 buffer2[5];//切换Page_0_R53
	u8 buffer1[5];
	u8 Gamma_buffer[6];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	//SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	//delay_ms(15);
	
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {						
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  page20
		buffer1[3] = 0x38;
		buffer1[4] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
    }
//	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
//	{		
//		buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0A;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}
//	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
//	{			
//		buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0B;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}	
//	else if((buffer[3]&0x0f) == 0x03)	//Normal_Gamma_4
//	{			
//		buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
//		buffer1[3] = 0x38;
//		buffer1[4] = 0x0C;
//        SSD2828_W_Array(SigMode,channel,buffer1,0);
//	}
	else if((buffer[3]&0x0f) == 0x04)	//HBM_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  page1E
		buffer1[3] = 0x38;
		buffer1[4] = 0x1E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x05)	//AOD_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA2  page2B
		buffer1[3] = 0x38;
		buffer1[4] = 0x2B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	

    for( u8 ix = 0;ix<((buffer[2]-1)/2);ix++)			 	
    {  	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
    
	Mipi_Read(pdev,SigMode,channel,ix+1,6,Buffer_ReadCode);   // i+1 寄存器地址，读取6个
		
	memcpy(Gamma_buffer,Buffer_ReadCode,6); //gamma Buffer
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);  
		
	    ILI8818_Gamma[0][ix]=(Gamma_buffer[0])<<8|(Gamma_buffer[1]); //R Gamma值读写缓存区	
		ILI8818_Gamma[1][ix]=(Gamma_buffer[2])<<8|(Gamma_buffer[3]); //R Gamma值读写缓存区	
		ILI8818_Gamma[2][ix]=(Gamma_buffer[4])<<8|(Gamma_buffer[5]); //R Gamma值读写缓存区	

		if((buffer[3]&0x70)==0x10)		//0x10 Red_Register
		{
            buffer[4+ix*2]=ILI8818_Gamma[0][ix]>>8;  
			buffer[4+ix*2+1]=ILI8818_Gamma[0][ix]&0XFF;  
		}
        else if((buffer[3]&0x70)==0x20)	//0x20 Green_Register
		{
            buffer[4+ix*2]=ILI8818_Gamma[1][ix]>>8;  
			buffer[4+ix*2+1]=ILI8818_Gamma[1][ix]&0XFF;  
		}
        else if((buffer[3]&0x70)==0x30)	//0x30 Blue_Register
		{
            buffer[4+ix*2]=ILI8818_Gamma[2][ix]>>8;  
			buffer[4+ix*2+1]=ILI8818_Gamma[2][ix]&0XFF;  
		}
    }
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??? Gamma??  :ok 
	
}




static void ILI8818_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u8 R_30,R_31,R_32,R_33,R_34,R_35,R_36,R_E6,R_E7;
	u8 ILI8818_OTP[22]={0xFE,0x42,0x03,0x10,0x04,0x32,0x05,0x10,0x06,0x32,0x07,0x10,0x08,0x32,0x09,0x10,0x0A,0x32,0x0B,0x10,0x0C,0x32};
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0xfffd);
	
	delay_ms(5);
	
	if(buffer[4]==6) //RCN
	{
		buffer1[0] = 0x02;
		buffer1[1] = 0xFE;
		buffer1[2] = 0xD0;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  				
		buffer1[0] = 0x02;
		buffer1[1] = 0x99;
		buffer1[2] = 0x20;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  	
		buffer1[0] = 0x02;
		buffer1[1] = 0xFE;
		buffer1[2] = 0x40;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  		
		buffer1[0] = 0x02;
		buffer1[1] = 0x9A;
		buffer1[2] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  								
	}
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xFE;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  		
	
	buffer1[0] = 0x01;
	buffer1[1] = 0x28;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 	
	delay_ms(50);
	buffer1[0] = 0x01;
	buffer1[1] = 0x10;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 	
	delay_ms(50);	
	if(buffer[4]==1 || buffer[4]==2|| buffer[4]==3||buffer[4]==4)
	{
	for(i=0;i<22;i++)//ILI8818新增
	{
		buffer1[0]=0x02;
		buffer1[1]=ILI8818_OTP[i];
		buffer1[2]=ILI8818_OTP[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(5);
		i++;
	}
	
	buffer1[0] = 0x02;	
	buffer1[1] = 0xFE;
	buffer1[2] = 0x40;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
	delay_ms(50);
	buffer1[0] = 0x02;
	buffer1[1] = 0xF2;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0			
	delay_ms(100);
	if(buffer[4]==1)//GAM1-50H
	{
		buffer1[0] = 0x02;
		if(buffer[5]==1) //gamma1
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x01;
		}
		else if(buffer[5]==5) //gamma5
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x10;		
		}
		else if(buffer[5]==9) //gamma9
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x01;		
		}
		else if(buffer[5]==13) //gamma13
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x10;	
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);					
	}
	//---------------------------------------------------		
	else if(buffer[4]==2) //52
	{
		buffer1[0] = 0x02;
		if(buffer[5]==2) //gamma2
		{							
			buffer1[1] = 0xEE;
			buffer1[2] = 0x02;
		}
		else if(buffer[5]==6) //gamma6
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x20;	
		}
		else if(buffer[5]==10)//gamma10
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x02;			
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	else if(buffer[4]==3)			//GAM1-54H	
	{
		buffer1[0] = 0x02;
		if(buffer[5]==3) //gamma3
		{				
			buffer1[1] = 0xEE;
			buffer1[2] = 0x04;   
		}
		else if(buffer[5]==7) //gamma7
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x40;   		
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	else if(buffer[4]==4)			//GAM1-56H	
	{
		buffer1[0] = 0x02;
		if(buffer[5]==4) //gamma4
		{	
			buffer1[1] = 0xEE;
			buffer1[2] = 0x08;
		}
		else if(buffer[5]==8) //gamma8
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x80;		
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
	}
}
	
	else if(buffer[4]==5)		//PAGE-A0/D0/40/60/70/90/72/74/F2
	{
		buffer1[0] = 0x02;			
		if(buffer[3]==1)
		{
			buffer1[0] = 0x02;	//切fe,D0
			buffer1[1] = 0xFE;
			buffer1[2] = 0xD0;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xE5;
			buffer1[2] = 0x03; //P-D0/A0
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			buffer1[1] = 0xF6;
			buffer1[2] = 0x17;  //P-40/60/70/90
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xF8;
			buffer1[2] = 0x20; //P-72
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			buffer1[1] = 0xEC;
			buffer1[2] = 0x08; //P-74
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xF0;
			buffer1[2] = 0x04; //P-F2
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if(buffer[3]==2)
		{
			buffer1[0] = 0x02;	//切fe,D0
			buffer1[1] = 0xFE;
			buffer1[2] = 0xD0;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xE4;
			buffer1[2] = 0x02;//P-D0
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			buffer1[1] = 0xF7;
			buffer1[2] = 0x17;  //P-40/60/70/90
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xF9;
			buffer1[2] = 0x20; //P-72
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			buffer1[1] = 0xEC;
			buffer1[2] = 0x10; //P-74
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xF0;
			buffer1[2] = 0x08; //P-F2
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
		}
	}
	else if(buffer[4]==6)		//20\22\26
	{
		if(buffer[3]==1)
		{	
			buffer1[1] = 0xF8;
			buffer1[2] = 0x16;
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
		else if(buffer[3]==2)
		{
			buffer1[1] = 0xF9;
			buffer1[2] = 0x16;
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
	}
	else if(buffer[4]==7) //RCN
	{
		if(buffer[3]==1)
		{				
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
			buffer1[1] = 0xF8;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if(buffer[3]==2)
		{
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
			buffer1[1] = 0xF8;
			buffer1[2] = 0x80;
			SSD2828_W_Array(SigMode,channel,buffer1,0);			
		}
	}
	//----------------
	buffer1[0] = 0x02;
	buffer1[1] = 0xFE;
	buffer1[2] = 0x40;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
	delay_ms(50);
	buffer1[0] = 0x02;//MTPCTR
	buffer1[1] = 0xF2;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);    
	buffer1[0] = 0x02;
	buffer1[1] = 0xF3;
	buffer1[2] = 0xA5;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  
	buffer1[0] = 0x02;
	buffer1[1] = 0xF4;
	buffer1[2] = 0x5A;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  
	buffer1[0] = 0x02;
	buffer1[1] = 0xF5;
	buffer1[2] = 0x3C;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  		
	delay_ms(1500);         // wait for >400 ms   
	buffer[4] = Uart_Error_None;
	//buffer[3] = Uart_Error_None;
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ?OTP??  :ok		
}
static void ILI8818_AutoGamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	

	u8 ix;	
	u8 Gamma_buffer[8];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data); //低速
	delay_ms(5);
	ix=buffer[5];
	ILI8818_Gamma[0][ix]=buffer[4]<<8|buffer[6]; //R
	ILI8818_Gamma[1][ix]=buffer[8]<<8|buffer[10]; //G
	ILI8818_Gamma[2][ix]=buffer[12]<<8|buffer[14]; //B
	Gamma_buffer[0]=7;
	Gamma_buffer[1]=ix+1; //写入地址从1开始
	Gamma_buffer[2]=ILI8818_Gamma[0][ix]>>8;
	Gamma_buffer[3]=ILI8818_Gamma[0][ix]&0XFF;	
	Gamma_buffer[4]=ILI8818_Gamma[1][ix]>>8;	
	Gamma_buffer[5]=ILI8818_Gamma[1][ix]&0XFF;	
	Gamma_buffer[6]=ILI8818_Gamma[2][ix]>>8;	
	Gamma_buffer[7]=ILI8818_Gamma[2][ix]&0XFF;
	SSD2828_W_Array(SigMode,channel,Gamma_buffer,0);																			
    delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);//低速下写完，切换回高速	
	
	
}
static void ILI8818_HSRead_Register_All(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

	u8 buffer1[5];
	u8 Gamma_buffer[6];
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	delay_ms(5);
	
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {						
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  page20
		buffer1[3] = 0x38;
		buffer1[4] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
	{		
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
		buffer1[3] = 0x38;
		buffer1[4] = 0x0A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
		buffer1[3] = 0x38;
		buffer1[4] = 0x0B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}	
	else if((buffer[3]&0x0f) == 0x03)	//Normal_Gamma_4
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
		buffer1[3] = 0x38;
		buffer1[4] = 0x0C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x04)	//HBM_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  pageE
		buffer1[3] = 0x38;
		buffer1[4] = 0x1E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x05)	//AOD_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA2  pageF
		buffer1[3] = 0x38;
		buffer1[4] = 0x2b;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	} 
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); //LP_B7_Data|0x0080
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);//返回两个字节；
	for( u8 ix = 0;ix<((buffer[2]-1)/3/2);ix++) 
    {	  				
		Mipi_Read(pdev,SigMode,channel,ix+1,6,Buffer_ReadCode);   // i+1 寄存器地址，读取6个		
	    memcpy(Gamma_buffer,Buffer_ReadCode,6); //gamma Buffer
		ILI8818_Gamma[0][ix]=(Gamma_buffer[0])<<8|(Gamma_buffer[1]); //R Gamma值读写缓存区	
		ILI8818_Gamma[1][ix]=(Gamma_buffer[2])<<8|(Gamma_buffer[3]); //G Gamma值读写缓存区	
		ILI8818_Gamma[2][ix]=(Gamma_buffer[4])<<8|(Gamma_buffer[5]); //B Gamma值读写缓存区	
        delay_ms(3);
        buffer[4+i*2]=Gamma_buffer[0];
		buffer[4+i*2+1]=Gamma_buffer[1];
        buffer[4+i*2+48]=Gamma_buffer[2];
		buffer[4+i*2+48+1]=Gamma_buffer[3];
		buffer[4+i*2+96]=Gamma_buffer[4];
		buffer[4+i*2+48+1]=Gamma_buffer[5];
    }
    
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);


}
//---------------------------------------------------------------------------------------------------------------
static void ILI8818_HSRead_Register_Sample(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	           
}


//进入处理
void ProcessForIc6F( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;	
	switch(buffer[1])
	{
		
		case 0x01:                                      //写寄存器
				ILI8818_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器		
				ILI8818_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;
		case 0x23://读取所有寄存器值 		
				ILI8818_HSRead_Register_All(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;
		case 0x33://读取所有寄存器值 		
//				ILI8818_HSWrite_Register_All(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;		
//		case 0x03:                                      //Gamma OTP
//				ILI8818_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
//				break;
		case 0x08:   									//写Gamma				
				ILI8818_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x09:                                      //读 Gamma
				ILI8818_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;
		case 0xDD:                                      // AutoGamma
				ILI8818_AutoGamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;

        
        
		default:break;
	}			
}
